package com.bright.hxj.qchz.controller;

import com.bright.ghj.common.cache.RedisCache;
import com.bright.ghj.common.pojo.bo.RestResult;
import com.bright.hxj.qchz.pojo.bo.DuijieReturnMsg;
import com.bright.hxj.qchz.pojo.po.Dist;
import com.bright.hxj.qchz.pojo.po.Duijie_ShiJieShouUser;
import com.bright.hxj.qchz.service.Duijie_QCHZ_DataManager;
import com.bright.hxj.qchz.service.Duijie_ShengJieShou_QCHZManager;
import com.bright.hxj.qchz.service.Duijie_ShiJieShouDeployManager;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * @Author hxj
 * @Date 2025/1/6 11:30
 * @Description
 */
@RequestMapping("/dataSync")
@RestController
@RequiredArgsConstructor
@Slf4j
public class DataSyncController {

    private final Duijie_ShengJieShou_QCHZManager qchzManager;
    private final Duijie_QCHZ_DataManager dataManager;
    private final RedisCache redisCache;

    @GetMapping("/test")
    public RestResult test() {
        Duijie_ShiJieShouUser user = new Duijie_ShiJieShouUser();
        user.setUserName("gzs_th");
        user.setDistNo("0106");
        List<Map<String, Object>> data = new ArrayList<>();
        data.add(Collections.singletonMap("id", 1));
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("ztid", "010651001");
        jsonObject.put("data", data);
//        qchzManager.insertOrUpdate(user, jsonObject, "CQTZ");
        DuijieReturnMsg returnMsg = qchzManager.processDataAsync(user, jsonObject, "CQTZ");
        return RestResult.success(returnMsg);
    }

    @ResponseBody
    @RequestMapping("/redis/put")
    public String redisPut(String key, String value) {
        redisCache.setCacheObject(key, value);
        return "值已放入";
    }

    @ResponseBody
    @RequestMapping("/redis/get")
    public Object redisGet(String key) {
        return redisCache.getCacheObject(key);
    }

    @ResponseBody
    @RequestMapping("/redis/put/list")
    public String redisPutList() {
        List<Map<String, Object>> dists = Collections.singletonList(Collections.singletonMap("aa", 20));
        redisCache.setCacheObject("dists", dists);

        Object obj = redisCache.getCacheObject("dists");
        List<Map<String, Object>> dists1 = (List<Map<String, Object>>) obj;

//        redisCache.appendCacheList("distList", dists);
//        List<Dist> distList = redisCache.getCacheList("distList");
        // 如果是setCacheObject赋值 则不能用getCacheList取
//        List<Dist> distList = redisCache.getCacheList("dists");

        return dists1.toString();
    }

    @ResponseBody
    @RequestMapping("/test/common/mapper")
    public String testCommonMapper() {
        dataManager.initYears();
        return "ok";
    }

}
